# d ;
****************************************;	
*This program is used to add a row indicating the use of sample id when using areg;
cap program drop add_lab_FE;
program define add_lab_FE;
	matrix V = [vecdiag(e(V)),1];
	matrix V=diag(V);
	matrix b=[e(b),1];
	matrix colnames b= `:  colnames e(b)' `1';
	matrix colnames V= `:  colnames e(V)' `1';
	erepost b=b V=V	,rename;
end;
****************************************;	
*This program calculates summary stats for regression tables;
cap program drop sum_stats;
program define sum_stats;
	sum Diri ;
	estadd  scalar mean_Diri=r(mean) ;
	sum aadt_10000  ;
	estadd  scalar mean_aadt=r(mean) ;
	sum I2_t ;
	estadd  scalar mean_I2_t=r(mean) ;
	sum I2_t_exp_I2  ;
	estadd  scalar mean_I2_t_exp=r(mean) ;
	end;
****************************************;	
*This program plots year dummies;
cap program drop plot_dummies;
program define plot_dummies;
	preserve;
	*save se to disk;
	mata: se=sqrt(diagonal(st_matrix("e(V)"))); // converts e(V) into se and placed in mata matrix se
	clear;
	getmata se, force ;
	gen counter=_n;
	save se,replace;
	
	*save coefficients to disk and organize;
	mat beta=e(b);
	mat list beta;
	clear;
	set obs 1;
	svmat double beta;
	*file in memory is a single row year effects from regression;
	gen dummy=1;
	reshape long beta,  i(dummy) j(year);
	gen counter=_n;
	*merge with se;
	merge 1:1 counter using se;
	drop if counter>17;
	drop if beta==. | beta==0;
	drop _merge counter;
	*just keep year X expenditure coefficients;
	drop if year==.;
	replace year = year+1991;
	sort year;
	*file in memory gives bin, mean y and se;
	gen up5 = beta+1.96*se;
	gen down5 = beta-1.96*se;

	save beta, replace;
	*file in memory is a list of year by year inverse prices and CI's;

	*settings for plots;
	local pdf_plot_settings = 	`"bgcolor(white) 
					graphregion(color(white)) plotregion(ls(none))  
								xmtick(1992(1)2008) 
								ymtick(##5)
								xlabel(1992(4)2008)
					xtitle("")
					legend(off)
					scale(*1.5)
					
					"';
					twoway rcap up5 down5 year, lstyle(ci)|| scatter beta year, `pdf_plot_settings'; 
	graph export "temp_t.pdf", replace;
	erase se.dta;
	erase beta.dta;
	save "temp_betas.dta", replace;
	restore;
end;
# d ;
cap program drop plot_dummies_cons;
program define plot_dummies_cons;
args y_var controls;
	matrix time_cons=J(17,3,.);
	matrix colnames time_cons=year beta var;
	local n=1;
	
	if "`controls'"==""{;
		forvalues year=1992(1)2008{;
				reg `y_var' I2_t_x* ,  cluster(state);
				nlcom _b[I2_t_x`year']+_b[_cons], post;
				mat  time_cons[`n',1]=`year';
				mat  time_cons[`n',2]=e(b);
				mat  time_cons[`n',3]=e(V);
				local n=`n'+1;
		}		;
	};
	
	
		
	if "`controls'"=="state"{;
		forvalues year=1992(1)2008{;
				
				areg Diri  I2_t_x*   ,  cluster(state) abs(state ) ;;
				nlcom _b[I2_t_x`year']+_b[_cons], post;
				mat  time_cons[`n',1]=`year';
				mat  time_cons[`n',2]=e(b);
				mat  time_cons[`n',3]=e(V);
				local n=`n'+1;
		}		;
	};
	
	
	
	if "`controls'"=="id"{;
		forvalues year=1992(1)2008{;
				
				areg Diri  I2_t_x*   ,  cluster(state) abs(id) ;
				nlcom _b[I2_t_x`year']+_b[_cons], post;
				mat  time_cons[`n',1]=`year';
				mat  time_cons[`n',2]=e(b);
				mat  time_cons[`n',3]=e(V);
				local n=`n'+1;
		}		;
	};
	
	
			preserve ;
				clear ;
				svmat time_cons, names(col) ;
				gen se=var^(1/2);
				gen up5 = beta+1.96*se;
				gen down5 = beta-1.96*se;

				save beta`y_var', replace;
				*file in memory is a list of year by year inverse prices and CI's;

				*settings for plots;
				local pdf_plot_settings = 	`"bgcolor(white) 
								graphregion(color(white)) plotregion(ls(none))  
								xmtick(1992(1)2008) 
								ymtick(##5)
								xlabel(1992(4)2008)
								legend(off)
								xtitle("")
								
								scale(*1.5)"';
				twoway rcap up5 down5 year, lstyle(ci)|| scatter beta year, `pdf_plot_settings'; 
				graph export "temp_t.pdf", replace;
				
				
			restore ;
end;



# d cr ;
